<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Util\Common;
use App\Http\Util\Err;
use App\Http\Util\HomeTool;
use App\Models\Banner;
use App\Models\Team;
use App\Models\TestRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RecordController extends Controller
{

    /**获取列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {

        $page = request()->only(['page', 'limit', 'keyword','sn']);
        $list = TestRecord::where(function ($query) use ($page) {
            if (array_key_exists('keyword', $page) && $page['keyword']) {
                $query->where('mobile',$page['keyword'] );
            }
            if (array_key_exists('sn', $page) && $page['sn']) {
                $query->where('sn',$page['sn'] );
            }
        })->orderBy('id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get();
        $count = TestRecord::where(function ($query) use ($page) {
            if (array_key_exists('keyword', $page) && $page['keyword']) {
                $query->where('mobile',$page['keyword'] );
            }
            if (array_key_exists('sn', $page) && $page['sn']) {
                $query->where('sn',$page['sn'] );
            }
        })->count();
        return response()->json(['data' => $list, 'total' => $count, 'code' => 200]);
    }

    /**删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $id = $request->get('id');
        if (!$id) {
            return response()->json(['code' => 1001, 'msg' => '无此条信息']);
        }
        $model = TestRecord::find($id);
        $model->delete();
        return response()->json(['code' => 200, 'msg' => '删除成功']);
    }
}
